package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.dto.DeviceMonitorBindQueryVo;
import com.workcell.workdata.xthings.dto.DeviceMonitorQueryVo;
import com.workcell.workdata.xthings.dto.PageVo;
import com.workcell.workdata.xthings.dto.SerialDeviceMonitorBindDto;
import com.workcell.workdata.xthings.dto.SerialDeviceMonitorDto;
import com.workcell.workdata.xthings.entity.BaseSerialDevice;
import com.workcell.workdata.xthings.mapper.BaseSerialDeviceMapper;
import com.workcell.workdata.xthings.service.BaseSerialDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Admin
 * @description 针对表【t_base_serial_device(串口设备表)】的数据库操作Service实现
 * @createDate 2023-03-17 11:36:54
 */
@Service
@RequiredArgsConstructor
public class BaseSerialDeviceServiceImpl extends ServiceImpl<BaseSerialDeviceMapper, BaseSerialDevice>
        implements BaseSerialDeviceService {
    private final BaseSerialDeviceMapper baseSerialDeviceMapper;

    @Override
    public Page<BaseSerialDevice> getBaseSerialDeviceVaguePage(BaseVo baseVo) {
        LambdaQueryWrapper<BaseSerialDevice> baseSerialDeviceCondition = new QueryWrapper<BaseSerialDevice>().lambda()
                .and(ObjectUtil.isNotEmpty(baseVo.getKeyWord()), i -> i
                        .like(BaseSerialDevice::getName, baseVo.getKeyWord())
                        .or()
                        .like(BaseSerialDevice::getManufacturer, baseVo.getKeyWord()))
                .eq(ObjectUtil.isNotEmpty(baseVo.getState()), BaseSerialDevice::getState, baseVo.getState())
                .and(ObjectUtil.isNotEmpty(baseVo.getStartTime()) || ObjectUtil.isNotEmpty(baseVo.getEndTime())
                        , i -> i.between(BaseSerialDevice::getCreateTime, baseVo.getStartTime(), baseVo.getEndTime()));
        Page<BaseSerialDevice> page = new Page<>(baseVo.getPageNum(), baseVo.getPageSize());
        page.addOrder(OrderItem.desc("create_time"));
        return this.page(page, baseSerialDeviceCondition);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateBaseSerialDeviceById(BaseSerialDevice baseSerialDevice) {
        BaseSerialDevice baseSerialDeviceOld = this.getById(baseSerialDevice.getId());
        Assert.notNull(baseSerialDeviceOld, "无此条记录，请核实您的操作");
        Assert.equals(HardwareStateEnum.DRAF.getCode(), baseSerialDeviceOld.getState(), "状态为草稿才能修改");
		baseSerialDevice.setState(null);
        return this.updateById(baseSerialDevice);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStateById(BaseSerialDevice baseSerialDevice) {
        Assert.notNull(baseSerialDevice.getId(), "ID不能为空");
        Assert.notNull(baseSerialDevice.getState(), "状态不能为空");

        BaseSerialDevice baseSerialDeviceOld = this.getById(baseSerialDevice.getId());
        Assert.notNull(baseSerialDeviceOld, "无此条记录，请核实您的操作");
        if (StrUtil.equalsAny(baseSerialDeviceOld.getState(), HardwareStateEnum.INUSE.getCode(), HardwareStateEnum.SUSPEND.getCode())) {
            Assert.isFalse(StrUtil.equals(HardwareStateEnum.DRAF.getCode(), baseSerialDevice.getState()),
                    "当状态为使用中或暂停中时，不能删除该条记录");
        }

        LambdaUpdateWrapper<BaseSerialDevice> lambdaUpdateWrapper = Wrappers.lambdaUpdate(BaseSerialDevice.class)
                .eq(BaseSerialDevice::getId, baseSerialDevice.getId())
                .set(BaseSerialDevice::getState, baseSerialDevice.getState());
        return this.update(lambdaUpdateWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeBaseSerialDeviceById(String dtId) {
        Assert.notNull(dtId, "ID不能为空");
        BaseSerialDevice baseSerialDevice = this.getById(dtId);
        Assert.notNull(baseSerialDevice, "无此条记录，请核实您的操作");
        Assert.isFalse(
                StrUtil.equalsAny(baseSerialDevice.getState(), HardwareStateEnum.INUSE.getCode(), HardwareStateEnum.SUSPEND.getCode()),
                "当状态为使用中或暂停中时，不能删除该条记录"
        );
        return this.removeById(dtId);
    }

    @Override
    public PageInfo<SerialDeviceMonitorDto> serialDeviceMonitorInfoPage(PageVo pageVO, DeviceMonitorQueryVo deviceMonitorQueryVo) {
        com.github.pagehelper.Page<SerialDeviceMonitorDto> startPage = PageHelper.startPage(pageVO);
        List<SerialDeviceMonitorDto> list = baseSerialDeviceMapper.serialDeviceMonitorInfoList(deviceMonitorQueryVo);
        PageInfo<SerialDeviceMonitorDto> pageInfo = new PageInfo<>(list);
        startPage.close();;
        return pageInfo;
    }

    @Override
    public PageInfo<SerialDeviceMonitorBindDto> serialDeviceMonitorBindPage(PageVo pageVO, DeviceMonitorBindQueryVo deviceMonitorBindQueryVo) {
        com.github.pagehelper.Page<SerialDeviceMonitorDto> startPage = PageHelper.startPage(pageVO);
        List<SerialDeviceMonitorBindDto> list = baseSerialDeviceMapper.serialDeviceMonitorBindPage(deviceMonitorBindQueryVo);
        PageInfo<SerialDeviceMonitorBindDto> pageInfo = new PageInfo<>(list);
        startPage.close();
        return pageInfo;
    }

}




